<?php
class common_region_concept extends system_concept_abstract {
	
	public function init() {	
		$this->setModel('common_region_model', 'region');
		$this->setModelCols('region', array('id', 'name', 'ename', 'zipcode'));
	}

	public function save($row) {
		if($row->getName() == null) {
			throw new common_exception("Name can not be empty.");
		}
		if($row->getId()) {
			if($this->fetch(array('region.id != ?' => $row->getId(), 'region.name = ?' => $row->getName()))) {
				throw new common_exception("Name ({$row->getName()}) is already existing.");
			}
			$this->getModel('region')->update($row->getModelData('region'), array('id=?' => $row->getId()));
			return $row;
		}
		if($this->fetch(array('region.name = ?' => $row->getName()))) {
			throw new common_exception("Name ({$row->getName()}) is already existing.");
		}
		$id = $this->getModel('region')->insert($row->getModelData('region'));
		$row->setId($id);
		return $row;
	}
	
	public function delete($row) {
		$concept = new common_region_city_concept();
		if($concept->fetchCount(array('city.province_id = ?' => $row->getId()))) {
			throw new common_exception("This Province can not be deleted,Please delete all cities in the Province first.");
		}
		parent::delete($row);
	}

}